package com.carltechnology.mahjonggamecaculation.dao.impl;

import android.database.Cursor;

import com.carltechnology.mahjonggamecaculation.dao.IMahjongDataDao;
import com.carltechnology.mahjonggamecaculation.dao.IMahjongGameDataSourceComponent;
import com.carltechnology.mahjonggamecaculation.mode.IDetailGameData;
import com.carltechnology.mahjonggamecaculation.mode.IGameData;
import com.carltechnology.mahjonggamecaculation.mode.IGameRuntimeData;
import com.carltechnology.mahjonggamecaculation.mode.impl.GameDataMode;
import com.carltechnology.mahjonggamecaculation.mode.impl.GameDetailDataMode;
import com.carltechnology.mahjonggamecaculation.mode.impl.GameRuntimeDataMode;

import rx.Subscriber;

/**
 * Created by WZTCM on 2016/6/28.
 */
public class MahjongGameDataSourceComponentComponent implements IMahjongGameDataSourceComponent {
    private IMahjongDataDao mDao;
    public MahjongGameDataSourceComponentComponent(IMahjongDataDao iDao) {
        mDao = iDao;
    }

    @Override
    public void getMahjongGameData(Subscriber<? super IGameData> subscriber) {
        Cursor cursor = null;
        try {
            cursor
                    = mDao.query(
                    String.valueOf(MahjongGameSqliteHelper.DATABASE_INFO.TABLE_GAMEDATA_NAME)
                    , "date desc");

            if (cursor.moveToFirst()){
                do {
                    GameDataMode mode = GameDataMode.transferCursor(cursor);
                    subscriber.onNext(mode);
                }while (cursor.moveToNext());
            }
        } catch (Exception e) {
            subscriber.onError(e);
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        // 完成订阅
        subscriber.onCompleted();
    }

    @Override
    public void getMahjongDetailData(final int id,Subscriber<? super IDetailGameData> subscriber){
        Cursor cursor = null;
        try {
            cursor =
                    mDao.queryById(
                            String.valueOf(MahjongGameSqliteHelper.DATABASE_INFO.TABLE_DETAILDATA_NAME)
                            , "date desc",id);

            if (cursor.moveToFirst()){
                do {
                    GameDetailDataMode mode = GameDetailDataMode.transferCursor(cursor);
                    subscriber.onNext(mode);
                }while (cursor.moveToNext());
            }
        } catch (Exception e) {
            subscriber.onError(e);
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        // 完成订阅
        subscriber.onCompleted();
    }

    @Override
    public void getMahjongRuntimeData(Subscriber<? super IGameRuntimeData> subscriber){
        Cursor cursor = null;
        try {
            cursor =
                    mDao.query(
                            String.valueOf(MahjongGameSqliteHelper.DATABASE_INFO.TABLE_RUNTIME_NAME)
                            , null);

            if (cursor.moveToFirst()){
                do {
                    GameRuntimeDataMode mode = GameRuntimeDataMode.transferCursor(cursor);
                    subscriber.onNext(mode);
                }while (cursor.moveToNext());
            }
        } catch (Exception e) {
            subscriber.onError(e);
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        // 完成订阅
        subscriber.onCompleted();
    }
}
